package utils 
{
	import flash.utils.Dictionary;
	
	public class Logger
	{
		private static var _loggers:Dictionary=new Dictionary();
		private static var _level:int=0;
		private var _name:String;
		private var _enable:Boolean=true;
		private var _stackInfoDepth:int=1;
		public static function getLogger(name:String):Logger{
			var _log:Logger=_loggers[name];
			if(_log==null){
				_log=new Logger(name);
			}
			return _log;
		}
		public function Logger(name:String){
			this._name=name
			if(_loggers[name]!=null){
				throw new Error("已存在Logger<"+name+">");
			}
			_loggers[name]=this;
		}
		public function warning(message:String,stackInfoDepth:int=0,level:int=2):void{
			log(new LogInfo("Warning",message,level),stackInfoDepth);
		}
		public function info(message:String,stackInfoDepth:int=0,level:int=1):void{
			log(new LogInfo("Info",message,level),stackInfoDepth);
		}
		public function error(message:String,stackInfoDepth:int=0,level:int=3):void{
			log(new LogInfo("Error",message,level),stackInfoDepth);
		}
		public function debug(message:String,stackInfoDepth:int=0,level:int=0):void{
			log(new LogInfo("Debug",message,level),stackInfoDepth);
		}
		private function log(logInfo:LogInfo,stackInfoDepth:int):void{
			if(!this._enable || logInfo.level<_level){
				return;
			}
			var endIndex:int=stackInfoDepth<=-1?-1:(stackInfoDepth==0?2+_stackInfoDepth:2+stackInfoDepth)
			trace(logInfo.uid+".<"+this._name+">"+logInfo.stackArray.slice(2,endIndex));
			trace(logInfo.type+":"+logInfo.message);
		}
		public function get name():String{
			return this._name;
		}
		public function get enable():Boolean{
			return this._enable;
		}
		public function set enable(value:Boolean):void{
			this._enable=value;
		}
		public function set stackInfoDepth(value:int):void{
			_stackInfoDepth=value;
		}
		public function get stackInfoDepth():int{
			return _stackInfoDepth;
		}
	}
	
}

	
class LogInfo{
		private static var _uidMaker:int=0;
		private var _uid:int=++_uidMaker;
		private var _stackArray:Array
		private var _message:String;
		private var _type:String;
		private var _level:int;
		
		public function LogInfo(type:String,message:String,level:int){
			this._type=type;
			this._message=message;
			this._level=level;
			var e:Error=new Error("LogInfo");
			_stackArray=e.getStackTrace().split("\n");
		}
		public function get stackArray():Array{
			return _stackArray;
		}
		public function get type():String{
			return this._type;
		}
		public function get message():String{
			return this._message;
		}
		public function get level():int{
			return this._level;
		}
		public function get uid():int{
			return this._uid
		}
	}